package com.flower.controller;

import com.flower.pojo.Flower;
import com.flower.pojo.Imgs;
import com.flower.pojo.Store;
import com.flower.pojo.User;
import com.flower.service.UserLoginService;
import com.flower.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private UserService userService;

    //登录
    @RequestMapping("/login.do")
    public Map<String,Object> loginUser(String account, String password, HttpSession session){
            Map<String,Object> map =new HashMap<>();
        User user=userLoginService.loginUserByAccount(account);
              if(user ==null){
                    map.put("code",201);
                    map.put("msg","用户不存在");
              }else if (!password.equals(user.getPassword())){
                  map.put("code",202);
                  map.put("msg","密码错误");
              }else{
                  map.put("code",200);
                  map.put("msg","登录成功");
                  map.put("role",user.getRole());
                  session.setAttribute("user",user);
              }
                return map;
    }

    //注册用户
    @RequestMapping("/register.do")
    public Map<String,Object> addUser(User user,HttpSession session){
        Map<String,Object> map = new HashMap<>();
        int result = userLoginService.addUser(user);

        if(result!=-1){
            map.put("code",200);
            map.put("msg","添加成功");
        }else{
            map.put("code",201);
            map.put("code","添加失败");
        }
        return map;
    }


    //更新用户个人信息
    @RequestMapping("/information_update")
    public Map<String,Object> UserInformationUpdate( User user, HttpSession session){
        Map<String,Object> map=new HashMap<>();
       // session.getAttribute("user");
        user.setUid(2);

        int result=userService.UserInformationUpdate(user);
        if(result!=-1){
            map.put("code",200);
            map.put("msg","信息添加成功");
        }else{
            map.put("code",201);
            map.put("msg","信息添加失败");
        }
        return map;
    }

    //通过ID查用户个人信息
    @RequestMapping("/querybyId")
    public Map<String,Object> queryById(HttpSession session){
        Map<String,Object> map=new HashMap<>();
        //int uid=session.getAttribute("user");
        int uid=1;
        User user=userService.queryById(uid);
        if(user!=null){
            map.put("code",200);
            map.put("msg","信息查询成功");
            map.put("data",user);
        }else {
            map.put("code",201);
            map.put("msg","信息查询失败");
        }
        return map;
    };
    //用户通过商店名模糊查询商店
    @RequestMapping("/querystorebyname")
    public  Map<String,Object> queryStoreByName(Integer currentPage,String sname){
        Map<String,Object> map=new HashMap<>();
        PageHelper.startPage(currentPage,3);
        List<Store> stores=userService.queryStoreByName(sname);
        PageInfo<Store> pageInfo=new PageInfo<>(stores);
        int total=(int)pageInfo.getTotal();
        int pageCount=pageInfo.getPages();
        if(stores==null||stores.isEmpty()){
            map.put("code",201);
            map.put("msg","没有相关店铺信息");
        }else{
            map.put("code",200);
            map.put("msg","店铺查询成功");
            map.put("data",stores);
            map.put("total",total);
            map.put("pageCount",pageCount);

        }
        return map;
    }
    //用户通过鲜花名模糊查询flower
    @RequestMapping("/queryflowerbyname")
    public  Map<String,Object> queryFlowerByName(Integer sid,Integer currentPage,String fname){
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> map1=new HashMap<>();
        map1.put("sid",sid);
        map1.put("fname",fname);
        PageHelper.startPage(currentPage,3);
        List<Flower> flowers=userService.queryFlowerByName(map1);
        PageInfo<Flower> pageInfo=new PageInfo<>(flowers);
        int total=(int)pageInfo.getTotal();
        int pageCount=pageInfo.getPages();
        if(flowers==null||flowers.isEmpty()){
            map.put("code",201);
            map.put("msg","没有相关店铺信息");
        }else{
            map.put("code",200);
            map.put("msg","店铺查询成功");
            map.put("data",flowers);
            map.put("total",total);
            map.put("pageCount",pageCount);
        }
        return map;
    }
    @RequestMapping("/queryFlowerByFid/{fid}")
    public Map<String,Object> queryFlowerByFid(@PathVariable("fid") Integer fid){
        Map<String,Object> map=new HashMap<>();
        Flower flower=userService.queryFlowerByFid(fid);
        List<Imgs> imgsList = userService.queryImgsByFid(fid);
        if(flower!=null &&imgsList.size()>0){
            map.put("code",200);
            map.put("msg","查询数据成功");
            map.put("flower",flower);
            map.put("imgs",imgsList);
        }else {
            map.put("code",201);
            map.put("msg","暂无数据");
        }
        return map;
    }
}
